package com.ychen.ruijee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ychen.ruijee.bean.Setmeal;
import com.ychen.ruijee.bean.SetmealDish;
import com.ychen.ruijee.common.R;
import com.ychen.ruijee.dto.DishDto;
import com.ychen.ruijee.dto.SetmealDto;
import com.ychen.ruijee.service.DishService;
import com.ychen.ruijee.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ychen@gotop.cn
 * @date 2024-06-03 23:55
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Resource
    private SetmealService setmealService;

    @GetMapping("/page")
    public R<Page<SetmealDto>> setmealPageInfo(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer pageSize,
                                               String name){
        Page<SetmealDto> result  = setmealService.setmealPageInfo(page,pageSize,name);
        return result == null ? R.error("查询失败") :R.success(result);
    }

    /**
     * 前端传递的是 json 所以使用 实体类来接受的时候 要加 @RequestBody 注解 目的是 json 序列化为 javaBean
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> saveSetmea(@RequestBody SetmealDto setmealDto){
        boolean flag  = setmealService.saveSetmeal(setmealDto);
        return  flag ? R.success("保存套餐信息成功") : R.error("保存套餐信息失败");
    }

    /**
     * 修改套餐时 数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    private R<SetmealDto> getSetmealDto(@PathVariable Long id){
        R<SetmealDto> result = setmealService.getSetmealDto(id);
        return  result;
    }
    //TODO 套餐修改

    /**
     * 批量删除
     * 前端使用的时 数组传递 后端使用 list来接受
     * @param ids
     * @return
     */
    @DeleteMapping
    private R<String> deleteSetmeal(@RequestParam("ids") List<Long> ids){
        boolean flag = setmealService.deleteSetmeal(ids);
        return  flag ? R.success("套餐批量删除成功"):R.error("套餐批量删除失败");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> appLookSetmeal(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());

        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);

    }




}
